﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace uchomeDesktop.listBox
{
    public class GListBox : ListBox
    {
        public ImageList ImageList;

        public GListBox()
        {
            this.DrawMode = DrawMode.OwnerDrawFixed;
            this.ItemHeight = 50;
        }

        protected override void OnDrawItem(System.Windows.Forms.DrawItemEventArgs e)
        {
            e.DrawBackground();
            e.DrawFocusRectangle();
            GListBoxItem item;
            Rectangle bounds = e.Bounds;

            try
            {
                item = (GListBoxItem)Items[e.Index];
                if (item.ImageIndex != -1 && ImageList != null)
                {
                    Size imageSize = ImageList.ImageSize;

                    ImageList.Draw(e.Graphics, bounds.Left + 5, bounds.Top + 5, item.ImageIndex);
                    e.Graphics.DrawString(item.Text, e.Font, new SolidBrush(e.ForeColor),
                      bounds.Left + imageSize.Width + 10, bounds.Top + 5);
                }
                else
                {
                    e.Graphics.DrawString(item.Text, e.Font, new SolidBrush(e.ForeColor),
                      bounds.Left, bounds.Top);
                }
            }
            catch
            {
                e.Graphics.DrawString("", e.Font, new SolidBrush(e.ForeColor),
                      bounds.Left, bounds.Top);
            }
            base.OnDrawItem(e);
        }
    }
}
